<?php
$skyscraper_banner = '<div style="display:inline-block; float:right; margin-right:-50px; margin-top:-40px">
                        <a  target="blank" href=http://www.envoyersmspro.com/affiliate/8278"><img class="envoyersmspro"  src="' . config_get('public_images') . '/envoyersmspro/banner-skyscraper.jpg" width="160" height="600"></a>
                     </div>';
$horizontal_banner = '<center><a target="blank" href=http://www.envoyersmspro.com/affiliate/8278"><img class="envoyersmspro" src="' . config_get('public_images') . '/envoyersmspro/banner-horizontal.jpg" width="468" height="60"></a></center><br/>';

function displaySmsAccountInfos($dataToShowIfAccountIsValid = null) {
    $res = '';
    ob_start();
    Tools_SmsGatewayObject::getAccountInfos();
    $xmlAccounts = ob_get_contents();
    ob_end_clean();
    $xmlTree = simplexml_load_string($xmlAccounts);
    if (is_object($xmlTree) && is_object($xmlTree->accounts) && is_object($xmlTree->accounts->account[0])) {
        $res .= '<img src="' . config_get('framework_images') . '/cmsimages/icon-success.png" /> <strong>Compte  SMS connecté</strong><br/><br/>';
        $res .= 'Nom du compte : ' . $xmlTree->accounts->account[0]->account_name . '<br/>';
        $res .= 'SMS restants : ' . $xmlTree->accounts->account[0]->sms_remaining . '<br/>';
        $res .= 'Coût unitaire : ' . Config_GeneralConfigObject::get('sms_cost') . ' €';
        $res .= $dataToShowIfAccountIsValid;
    } else {
        $res .= '<img src="' . config_get('framework_images') . '/cmsimages/icon-warning.png" /> ';
        $res .= '<strong>La fonctionnalité campagne SMS n\'est disponible que si vous avez connecté votre compte SMS</strong><br/><br/>';
        $res .= '<a href="' . Erra_System_PageBuilder::makeAjaxLink('mail-sms/expediteur-sms') . '">Cliquez ici</a> pour connectez votre compte EnvoyerSMSPro';
    }
    return $res;
}
?>
<div id="right">
    <div class="page-content">
        <div class="header-content mailsms-gradient">
            <table><tr><td><img src="<?= config_get('public_images') ?>/header/header_mail_sms.png"/>
                    </td><td>Gestion des mails / sms</td></tr></table>
        </div>
        <?php
        Config_GeneralConfigObject::loadConfiguration();
        ?>
        <div class="inner-content left-menu-inner">
            <ul class="split-right-menu">
                <li class="top-level client-list">
                    Pour les clients
                    <ul class="sub-item">
                        <li><a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/gestion-sms') ?>">Gestion SMS pour vos clients</a></li>
                        <li><a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/gestion-email') ?>">Gestion E-mail pour vos clients</a></li>
                    </ul>
                </li>
                <li class="top-level team-list">
                    Pour votre équipe
                    <ul class="sub-item">
                        <li><a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/team-gestion-sms') ?>">Gestion SMS pour votre équipe</a></li>
                        <li><a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/team-gestion-email') ?>">Gestion E-mail pour votre équipe</a></li>
                    </ul>
                </li>
                <li class="top-level campaign-list">
                    Campagnes mail et SMS
                    <ul class="sub-item">
                        <li><a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/campagne-email') ?>">Campagnes e-mail</a></li>
                        <li><a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/campagne-sms') ?>">Campagnes SMS</a></li>
                    </ul>
                </li>
                <li class="top-level sms-list">
                    Gérer paramètres expediteurs
                    <ul class="sub-item">
                        <li><a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/expediteur-sms') ?>">Pour les SMS</a></li>
                        <li><a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/expediteur-email') ?>">Pour les e-mails</a></li>
                    </ul>
                </li>
                <li class="top-level template-list">
                    Gérer les templates
                    <ul class="sub-item">
                        <a href="<?= Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') ?>">Templates des mails / SMS</a>
                    </ul>
                </li>
            </ul>
            <div class="split-content split-sms">
                <?php if ($GET_page == ''): ?>
                    <?= $skyscraper_banner ?>
                    <div style="margin-left:50px;">
                        <?= displaySmsAccountInfos() ?>
                        <img src="<?= config_get('public_images') ?>/page-mail-sms.jpg" />
                    </div>
                <?php elseif ($GET_page == 'gestion-sms'): ?>
                    <?= $skyscraper_banner ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_client_reminder') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_client_newrdv') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_client_moverdv') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_client_deleterdv') ?>
                <?php elseif ($GET_page == 'gestion-email'): ?>
                    <?= $skyscraper_banner ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('mail_client_reminder') ?>
                <?php elseif ($GET_page == 'team-gestion-sms'): ?>
                    <?= $skyscraper_banner ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_team_reminder') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_team_newrdv') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_team_moverdv') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_team_deleterdv') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('sms_team_internetonly') ?>
                <?php elseif ($GET_page == 'team-gestion-email'): ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('mail_team_reminder') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('mail_team_newrdv') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('mail_team_deleterdv') ?>
                    <?= Config_GeneralConfigObject::ajaxConfig('mail_team_moverdv') ?>
                <?php elseif ($GET_page == 'campagne-email'): ?>
                    <?= Msa_CampaignMailObject::getList()->_form('SaveAction/saveCampaign'); ?>
                <?php elseif ($GET_page == 'campagne-sms'): ?>
                    <?= $horizontal_banner ?>
                    <?=
                    displaySmsAccountInfos(Msa_CampaignSmsObject::getList()
                                    ->addAttribute('data-sms-cost', Config_GeneralConfigObject::get('sms_cost'))
                                    ->_form('SaveAction/saveCampaign'))
                    ?>
                <?php elseif ($GET_page == 'expediteur-email'): ?>
                    <?= $skyscraper_banner ?>
                    <div class="inline-config-item">
                        <?= Config_GeneralConfigObject::ajaxConfig('mailer_name') ?>
                    </div>
                    <div class="inline-config-item-save">
                        <div id="save-mailer-name" class="live-click pointer config-button" data-function="save_general_config" data-column="mailer_name">Sauvegarder</div>
                    </div>
                    <div class="inline-config-item">
                        <?= Config_GeneralConfigObject::ajaxConfig('mailer_address') ?>
                    </div>
                    <div class="inline-config-item-save">
                        <div id="save-mailer-address" class="live-click pointer config-button" data-function="save_general_config" data-column="mailer_address">Sauvegarder</div>
                    </div>   
                <?php elseif ($GET_page == 'expediteur-sms'): ?>

                    <div style="display:inline-block">               
                        <div style="display:inline-block">
                            <?= Config_GeneralConfigObject::ajaxConfig('sms_sendername') ?>
                        </div>
                        <div style="display:inline-block">
                            <div id="sms-sendername" class="live-click pointer config-button" data-function="save_general_config" data-column="sms_sendername">Sauvegarder</div>
                        </div>
                        <div style="clear:both"></div>
                        <div style="display:inline-block">
                            <?= Config_GeneralConfigObject::ajaxConfig('identifiant_sms') ?>
                        </div>
                        <div style="display:inline-block">
                            <div id="save-sms-identifier" class="live-click pointer config-button" data-function="save_general_config" data-column="identifiant_sms">Sauvegarder</div>
                        </div>
                        <div style="clear:both"></div>
                        <div style="display:inline-block">
                            <?= Config_GeneralConfigObject::ajaxConfig('mot_de_passe_sms') ?>
                        </div>
                        <div style="display:inline-block">
                            <div id="save-sms-password" class="live-click pointer config-button" data-function="save_general_config" data-column="mot_de_passe_sms">Sauvegarder</div>
                        </div>
                        <div style="clear:both"></div>
                        <div style="display:inline-block">
                            <?= Config_GeneralConfigObject::ajaxConfig('sms_cost') ?>
                        </div>
                        <div style="display:inline-block">
                            <div id="save-sms-password" class="live-click pointer config-button" data-function="save_general_config" data-column="sms_cost">Sauvegarder</div>
                        </div>
                        <br/>
                        <?php
                        ob_start();
                        Tools_SmsGatewayObject::getAccountInfos();
                        $xmlAccounts = ob_get_contents();
                        ob_end_clean();
                        $xmlTree = simplexml_load_string($xmlAccounts);
                        $res = '<br/>';
                        if (is_object($xmlTree) && is_object($xmlTree->accounts) && is_object($xmlTree->accounts->account[0])) {
                            $res .= '<img src="' . config_get('framework_images') . '/cmsimages/icon-success.png" /> <strong>Compte  SMS connecté</strong><br/><br/>';
                            $res .= 'Nom du compte : ' . $xmlTree->accounts->account[0]->account_name . '<br/>';
                            $res .= 'SMS restants : ' . $xmlTree->accounts->account[0]->sms_remaining . '<br/>';
                        } else {
                            $res .= '<img src="' . config_get('framework_images') . '/cmsimages/icon-warning.png" /> <strong>Veuillez entrer et sauvegarder votre identifiant et mot de passe pour activer la fonction SMS</strong>';
                        }
                        echo $res;
                        ?>
                    </div>
                    <?= $skyscraper_banner ?>

                <?php elseif ($GET_page == 'templates'): ?>
                    <?
                    $res = '';
                    $adminData = array();
                    $rdvData = array();
                    $smsData = array();
                    $boxData = array();
                    $adminData[] = array('title' => 'Mail mot de passe oublié (administrateur)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_AdminPasswordForgot.phtml'));
                    $adminData[] = array('title' => 'Mail inscription administrateur', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_AdminRegister.phtml'));
                    $adminData[] = array('title' => 'Mail mot de passe oublié (client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_ClientPasswordForgot.phtml'));
                    $adminData[] = array('title' => 'Mail inscription client', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_ClientRegister.phtml'));
                    $rdvData[] = array('title' => 'Mail annunlation client(à la demande du client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvCancel.phtml'));
                    $rdvData[] = array('title' => 'Mail annunlation client (raisons internes)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvCancelInternal.phtml'));
                    $rdvData[] = array('title' => "Mail d'annulation (pour les prestataires)", 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvCancelPresta.phtml'));
                    $rdvData[] = array('title' => 'Mail de confirmation rendez-vous (prestataire)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvConfirmPresta.phtml'));
                    $rdvData[] = array('title' => 'Mail de confirmation rendez-vous (client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvConfirmUser.phtml'));
                    $rdvData[] = array('title' => 'Mail de modification du rendez-vous (client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvEditClient.phtml'));
                    $rdvData[] = array('title' => 'Mail de modification du rendez-vous (prestataire)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvEditPresta.phtml'));
                    $rdvData[] = array('title' => 'Mail rappel du rendez-vous (client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvReminderClient.phtml'));
                    $rdvData[] = array('title' => 'Mail rappel du rendez-vous (prestataire)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Mail/Mail_RdvReminderPresta.phtml'));

                    $smsData[] = array('title' => 'SMS annulation du rendez-vous (prestataire)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Sms/Sms_RdvCancelPresta.phtml'));
                    $smsData[] = array('title' => 'SMS annulation du rendez-vous (client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Sms/Sms_RdvCancelUser.phtml'));
                    $smsData[] = array('title' => 'SMS Confirmation du rendez-vous (prestataire)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Sms/Sms_RdvConfirmPresta.phtml'));
                    $smsData[] = array('title' => 'SMS Confirmation du rendez-vous (client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Sms/Sms_RdvConfirmUser.phtml'));
                    $smsData[] = array('title' => 'SMS de modification du rendez-vous (client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Sms/Sms_RdvEditClient.phtml'));
                    $smsData[] = array('title' => 'SMS de modification du rendez-vous (prestataire)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Sms/Sms_RdvEditPresta.phtml'));
                    $smsData[] = array('title' => 'SMS rappel du rendez-vous (prestataire)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Sms/Sms_RdvReminderPresta.phtml'));
                    $smsData[] = array('title' => 'SMS rappel du rendez-vous (client)', 'key' => Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . base64_url_encode('Sms/Sms_RdvReminderUser.phtml'));

                    $boxData[] = array('title' => 'Templates administrateur', 'children' => $adminData);
                    $boxData[] = array('title' => 'Templates rendez-vous', 'children' => $rdvData);
                    $boxData[] = array('title' => 'Templates SMS', 'children' => $smsData);
                    $data = array(array('title' => 'Templates', 'key' => 'null', 'children' => $boxData));

                    $comboBoxSelection = Erra_Fields_MultiLevelComboBox::create('select_template')
                            ->setDescription('Selection du template à editer')
                            ->fill($data);
                    $template = 'Mail/Mail_AdminPasswordForgot.phtml';
                    if (strlen($GET_urlparameter)) {
                        $comboBoxSelection->setValue(Erra_System_PageBuilder::makeAjaxLink('mail-sms/templates') . '/' . $GET_urlparameter);
                        $template = base64_url_decode($GET_urlparameter);
                    }

                    $res .= $comboBoxSelection->editInstance();

                    // Write usable variables here
                    $file_contents = file_get_contents(DOCUMENT_ROOT . '/application/Views/' . 'Default' . $template);
                    $variables = array('{nom_complet}' => "Nom et prénom du client (ou du prestataire)",
                        '{admin_mail}' => "Adresse e-mail de l'administrateur inscrit",
                        '{admin_password}' => "Mot de passe de l'administrateur inscrit",
                        '{nom_etablissement}' => "Nom de votre établissement (défini dans l'onglet configuration)",
                        '{email_client}' => "Adresse e-mail du client inscrit",
                        '{mot_de_passe_client}' => "Mot de passe du client inscrit",
                        '{url_reservation}' => "URL de reservation de votre boutique",
                        '{date_reservation}' => "Date de la réservation",
                        '{heure_reservation}' => "Heure de la réservation",
                        '{nouvelle_date_reservation}' => "Date (modifiée) de la réservation",
                        '{nouvelle_heure_reservation}' => "Heure (modifiée) de la réservation");

                    if (strlen($file_contents)) {
                        $res .= 'La table des variables ci dessous vous permet d\'enrichir vos mails et SMS<br/>';
                        $res .= '<font color="red"><i>Pour les templates e-mail, il est impératif de démarrer leur création par <b>&lt;subject&gt; sujet du mail&lt;/subject&gt;</b></font>';
                        $res .= '<table class="variable-list">';
                        $res .= '<tr><th>Nom de la variable</th><th>Définition</th></tr>';
                        foreach ($variables as $key => $description) {
                            if (stristr($file_contents, $key)) {
                                $res .= '<tr><td>' . $key . '</td><td>' . $description . '</td></tr>';
                            }
                        }
                        $res .= '</table>';
                        // Set default message here
                        $res .= '<div id="default-template" class="pointer">';
                        $res .= 'Cliquez ici pour voir le template par défaut';
                        $res .= '</div>';
                        $res .= '<div id="default-template-content" style="display:none;">';
                        $res .= '<textarea id="select-default-template" rows="10" cols="70">' . $file_contents . '</textarea>';
                        $res .= '</div>';
                    }
                    // Write template windows here
                    $templateEditor = Tools_TemplateEditObject::getObject('template', 'CREATE', null, $template);
                    $res .= $templateEditor->_form('SaveAction/saveTemplate');

                    echo $res;
                    ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>